﻿layui.use('table', function () {
    var table = layui.table;
    //方法级渲染
    table.render({
        elem: '#LAY_table_user'
        , url: '/Message/GetMessageList'
        , cols: [[
            { checkbox: true },
            { field: 'id', title: 'ID', width: 80, sort: true },
            { field: 'content', title: '消息内容', width: 250 },
            { field: 'sendUserName', title: '消息发送人', width: 180},
            { field: 'sendTime', title: '消息发送时间', width: 180, templet: '#Time' },
            { field: 'readTime', title: '消息阅读时间', width: 180, templet: '#RTime' },
        ]]
        , id: 'id'
        , page: true
        , height: 630
    });
    //搜索
    var $ = layui.$, active = {
        reload: function () {
            var demoReload = $('#msg');
            table.reload('id', {
                where: {
                    IsSee: parseInt(demoReload.val())
                }
            });
        }
    };
    $('.demoTable .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    //标记为已读
    $("div").delegate('#unReadMsg','click',function (e) {
        // 获取选中列数据
        var checkStatus = table.checkStatus('id');
        if (checkStatus.data.length === 0) {
            layer.alert("没有选中项");
            return;
        }
        //循环获得id
        var Ids = [];
        for (var i = 1; i <= checkStatus.data.length; i++) {
            Ids.push({ Id: checkStatus.data[i - 1].id });
        }
        layer.confirm('你确定要标记为已读？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.ajax({
                url: "/Message/UpdateMessage",
                type: 'post',
                data: { Id: Ids },
                dataType: "json",
                async: "false",
                success: function (data) {
                    if (data.code === 0) {
                        layer.msg("更新成功", {
                            time: 2000 //20s后自动关闭
                            , icon: 1
                        });
                        setTimeout('myrefresh()', 800); //隔一秒刷新
                    }
                    else {
                        layer.msg("更新失败", {
                            time: 2000 //20s后自动关闭
                            , icon: 5
                        });
                    }
                }
            });
        });
    });
});